//
//  NoticePopHelper.m
//  TooToo
//
//  Created by liuning on 15/10/21.
//  Copyright © 2015年 MoHao. All rights reserved.
//

#import "NoticePopHelper.h"

#define NOTICE_POP_TAG 0x9a9b

@implementation NoticePopHelper

+(void)showNoticeText:(NSString *)text
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    
    if ([window viewWithTag:NOTICE_POP_TAG]) {
        return;
    }
    
    if ([NSString isEmptyString:text]) {
        return;
    }
    
    UIView * bgView = [[UIView alloc]init];
    bgView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
    bgView.layer.cornerRadius = 8;
    bgView.clipsToBounds = YES;
    [window addSubview:bgView];
    
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectZero];
    label.text = text;
    label.tag = NOTICE_POP_TAG;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:16];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.numberOfLines = 0;
    [bgView addSubview:label];
    
    float height = [text heightForFont:label.font width:150];
    label.frame = CGRectMake(0, 0, 180, 20*2+height);
    
    bgView.frame = CGRectMake(0, 0, 200, 20*2+height);
    bgView.center = window.center;
    label.center = CGPointMake(bgView.bounds.size.width/2.0f, bgView.bounds.size.height/2.0f);
    
    bgView.alpha = 0;
    
    [UIView animateWithDuration:0.3 animations:^{
        bgView.alpha = 1;
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:0.3 animations:^{
            bgView.alpha = 0;
        }completion:^(BOOL finished) {
            [bgView removeFromSuperview];
        }];
    });
    
}
+(BOOL)noticeIsShowing
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    if ([window viewWithTag:NOTICE_POP_TAG]) {
        return YES;
    }
    
    return NO;
}
@end
